<template>
    <div>
        <!-- 搜索栏 -->
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="知识库">
                    <div class="w-[250px]">
                        <el-input
                            v-model="queryParams.name"
                            class="w-[280px]"
                            placeholder="请输入知识库名称"
                            clearable
                            @keyup.enter="resetPaging"
                        />
                    </div>
                </el-form-item>
                <el-form-item label="拥有者">
                    <div class="w-[250px]">
                        <el-input
                            v-model="queryParams.user"
                            placeholder="请输入用户昵称/编号"
                            clearable
                            @keyup.enter="resetPaging"
                        />
                    </div>
                </el-form-item>
                <el-form-item label="状态值">
                    <el-select
                        v-model="queryParams.is_disable"
                        class="w-[180px]"
                        placeholder="全部"
                    >
                        <el-option value="" label="全部"/>
                        <el-option value="0" label="正常"/>
                        <el-option value="1" label="禁用"/>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPaging">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>

        <!-- 表格栏 -->
        <el-card v-loading="pager.loading" class="!border-none" shadow="never">
            <el-table :data="pager.lists" size="large" class="mt-4">
                <el-table-column label="ID" prop="id" min-width="60" />
<!--                <el-table-column label="图标" min-width="100">-->
<!--                    <template #default="{ row }">-->
<!--                        <el-avatar :size="50" :src="row.logo"/>-->
<!--                    </template>-->
<!--                </el-table-column>-->
                <el-table-column label="知识库名称" prop="name" min-width="200">
                    <template #default="{ row }">
                        <div class="flex items-center">
                            <el-avatar :size="50" :src="row?.logo" class="shrink-0" />
                            <div class="grid ml-2">
                                <el-popover
                                    :content="row?.user?.sn"
                                    effect="dark"
                                    placement="top-start"
                                >
                                    <template #reference>
                                        <p class="line-clamp-1">
                                            {{ row?.name }}
                                        </p>
                                    </template>
                                </el-popover>
                                <p class="text-xs text-tx-placeholder line-clamp-1">{{ row?.code }}</p>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="拥有者" prop="user" min-width="165">
                    <template #default="{ row }">
                        <div class="flex items-center">
                            <el-avatar :size="50" :src="row?.user?.avatar" class="shrink-0" />
                            <div class="grid ml-2">
                                <p class="line-clamp-1">{{ row?.user?.nickname }}</p>
                                <el-popover
                                    :content="row?.user?.sn"
                                    effect="dark"
                                    placement="top-start"
                                >
                                    <template #reference>
                                        <p class="text-xs text-tx-placeholder line-clamp-1">
                                            {{ row?.user?.sn }}
                                        </p>
                                    </template>
                                </el-popover>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="数据总量" prop="train_total" min-width="110">
                    <template #default="{ row }">
                        <el-tag type="info">{{ row?.train_total }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="向量模型" prop="vector_model" min-width="150">
                    <template #default="{ row }">
                        <el-tag type="info">{{ row?.vector_model }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="拆分模型" prop="splits_model" min-width="150">
                    <template #default="{ row }">
                        <el-tag type="info">{{ row?.splits_model }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="状态" prop="is_disable" min-width="80">
                    <template #default="{ row }">
                        <div @click="handleDisable(row.id, row.is_disable)">
                            <el-tag v-if="row.is_disable === 0" class="cursor-pointer">正常</el-tag>
                            <el-tag v-else type="danger" class="cursor-pointer">禁用</el-tag>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="create_time" min-width="180" />
                <el-table-column label="操作" width="190" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['agent:know:archive']"
                            type="primary"
                            link
                            @click="emits('listen', 'archive', row)"
                        >
                            文档集
                        </el-button>
                        <el-button
                            v-perms="['agent:know:detail']"
                            type="primary"
                            link
                            @click="handleEditor('detail', row)"
                        >
                            详情
                        </el-button>
                        <el-button
                            v-perms="['agent:know:delete']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <paging v-model="pager" @change="queryLists" />
            </div>
        </el-card>

        <!-- 详情弹窗 -->
        <pop-know-detail v-if="detailShow" ref="detailsRef" @close="detailShow = false" />
    </div>
</template>

<script setup lang="ts">
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import agentKnowApi from '@/api/agent/know/manage'
import PopKnowDetail from './_pop_know_detail.vue'

const emits = defineEmits(['listen'])

// 详情弹窗
const detailShow = ref(false)
const detailsRef = shallowRef<InstanceType<typeof PopKnowDetail>>()

// 查询参数
const queryParams = reactive({
    name: '',
    user: '',
    is_disable: ''
})

// 分页查询
const { pager, queryLists, resetParams, resetPaging } = usePaging({
    fetchFun: agentKnowApi.lists,
    params: queryParams
})

/**
 * 处理详情
 *
 * @param {string} type
 * @param {any} row
 * @returns {Promise<void>}
 */
const handleEditor = async (type: string, row?: any): Promise<void> => {
    detailShow.value = true
    await nextTick()
    detailsRef.value?.open(type, row)
}

/**
 * 处理删除
 *
 * @param {number} id
 * @returns {Promise<void>}
 */
const handleDelete = async (id: number): Promise<void> => {
    feedback.prompt('请输入删除原因', '删除知识库', {
        inputType: 'textarea',
        inputPattern: /.+/,
        inputErrorMessage: '删除原因不能为空',
    }).then(async (v) => {
        await agentKnowApi.delete(id, v.value)
        feedback.msgSuccess('删除成功')
        await queryLists()
    }).catch(() => {})
}

/**
 * 处理状态
 *
 * @param {number} id
 * @param {number} status
 */
const handleDisable = async (id: number, status: number) => {
    if (status === 0) {
        feedback.prompt('请输入禁用原因', '禁用知识库', {
            inputType: 'textarea',
            inputPattern: /.+/,
            inputErrorMessage: '禁用原因不能为空',
        }).then(async (v) => {
            await agentKnowApi.disable(id, v.value)
            feedback.msgSuccess('禁用成功')
            await queryLists()
        }).catch(() => {})
    } else {
        feedback.confirm('确定要解禁该知识库吗?')
            .then(async () => {
                await agentKnowApi.disable(id)
                feedback.msgSuccess('解禁成功')
                await queryLists()
            }).catch(() => {})
    }
}

onMounted(async () => {
    await queryLists()
})
</script>
